<?php
require_once ('Controller/Admin/Common.inc.php');
require_once ('View/Result.inc.php');
//require_once ('Model/GuestBookModel.inc.php');
require_once ('Model/GuestBookPresetMessageModel.inc.php');
require_once ('Model/LanguageModel.inc.php');
final class Controller extends common {
//	private $Loader;
	private $GuestBookPresetMessageModel;
	private $LanguageModel;
	private $diaplayrows = 20;
	public function execude () {
		$this->GuestBookPresetMessageModel = new GuestBookPresetMessageModel ($this->mysql);
		$trace = debug_backtrace ();
		if (isset ($trace[1])) {
			$this->Loader = $trace[1]['object'];
		}
		if ($this->checklogin ()) {
			if (array_key_exists ('action', $_REQUEST)) {
				switch ($_REQUEST['action']) {
					case 'delete':
						$this->deletePage ();
						break;
					case 'edit':
						$this->editPage ();
#						$this->deletePage ();
						break;
					case 'add':
						$this->addPage ();
						break;
					default:
						$this->listPage ();
						break;
				}
			} else {
				$this->listPage ();
			}
		}
	}

	private function NaddPage ($text = NULL, $lang_code = NULL, $id = NULL) {
		$messageaddtemplate = new template ();
		$messageaddtemplate->load ('html/GuestBookPresetMessageAdd.html');
		$messageaddform = $messageaddtemplate->dom->documentElement;
		$messageaddform = $this->template->dom->importNode ($messageaddform, true);

		if ($id == NULL) {
			$messageaddform->setAttribute ('action', sprintf ('?module=%s&action=add', $this->Loader->module));
			$node = $this->template->query ('table/tr/td/input[@id="editMessageButton"]', $messageaddform)->item (0);
			$node->parentNode->removeChild ($node);
			$node = $this->template->query ('table/tr/td/span[@id="editPresetMessage"]', $messageaddform)->item (0);
			$node->parentNode->removeChild ($node);
		} else {
			$messageaddform->setAttribute ('action', sprintf ('?module=%s&action=edit&id=%d', $this->Loader->module, $id));
			$node = $this->template->query ('table/tr/td/input[@id="addMessageButton"]', $messageaddform)->item (0);
			$node->parentNode->removeChild ($node);
			$node = $this->template->query ('table/tr/td/span[@id="newPresetMessage"]', $messageaddform)->item (0);
			$node->parentNode->removeChild ($node);
		}
		if ($text != NULL) {
			$node = $this->template->query ('table/tr/td/input[@name="text"]', $messageaddform)->item (0);
			$node->setAttribute ('value', $text);
		}

		$node = $this->template->query ('table/tr/td/select[@name="lang_code"]/option', $messageaddform)->item (0);
		$this->LanguageModel = new LanguageModel ($this->mysql);
		foreach ($this->LanguageModel->GetLanguageItems (0, $this->LanguageModel->GetLanguageCount ()) as $item) {
			$newnode = $node->cloneNode (true);
			if ($lang_code != NULL && $item[$this->LanguageModel->codeRowName] == $lang_code) {
				$newnode->setAttribute ('selected', 'selected');
			}
			$newnode->setAttribute ('value', $item[$this->LanguageModel->codeRowName]);
			$newnode->nodeValue = htmlspecialchars ($item[$this->LanguageModel->codeRowName]);
			$node->parentNode->appendChild ($newnode);
		}
		$this->template->content->appendChild ($messageaddform);
	}

	private function addPage () {
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$this->LanguageModel = new LanguageModel ($this->mysql);
			if ($_REQUEST['text'] != '' && $_REQUEST['lang_code'] != '' && $this->LanguageModel->CheckLanguageExistsByCode ($_REQUEST['lang_code'])) {
				$this->GuestBookPresetMessageModel->AddGuestBookPresetMessageItem ($_REQUEST['text'], $_REQUEST['lang_code']);
				$this->template->addWarningMesage ('Add a new Preset Message Successful');
				$this->listPage ();
			} else {
				$text = NULL;
				$lang_code = NULL;
				if ($_REQUEST['text'] == '') {
					$this->template->addWarningMesage ('Please input the description');
				} else {
					$text = $_REQUEST['text'];
				}
				if ($_REQUEST['lang_code'] == '') {
					$this->template->addWarningMesage ('Please select the language code');
				} else if (!$this->LanguageModel->CheckLanguageExistsByCode ($_REQUEST['lang_code'])) {
					$this->template->addWarningMesage ('Please select the correct language code');
				} else {
					$lang_code = $_REQUEST['lang_code'];
				}
				$this->NaddPage ($text, $lang_code);
			}
		} else {
			$this->NaddPage ();
		}
	}

	private function deletePage () {
		if (array_key_exists ('id', $_REQUEST) && ctype_digit ($_REQUEST['id'])) {
			$id = $_REQUEST['id'];
			if ($this->GuestBookPresetMessageModel->DelGuestBookPresetMessageItem ($id)) {
				$this->template->addWarningMesage ('Delete Successful');
			} else {
				$this->template->addWarningMesage ('Message not exists');
			}
			$this->listPage ();
		} else {
			$this->template->addWarningMesage ('wrong parameter');
		}
	}

	private function editPage () {
		if (array_key_exists ('id', $_REQUEST) && ctype_digit ($_REQUEST['id'])) {
			$id = $_REQUEST['id'];
			if (($GuestBookPresetMessageItem = $this->GuestBookPresetMessageModel->CheckGuestBookPresetMessageItemExist ($id)) != NULL) {
				if ($_SERVER['REQUEST_METHOD'] == 'POST') {
					$this->LanguageModel = new LanguageModel ($this->mysql);
					if ($_REQUEST['text'] != '' && $_REQUEST['lang_code'] != '' && $this->LanguageModel->CheckLanguageExistsByCode ($_REQUEST['lang_code'])) {
						$this->GuestBookPresetMessageModel->EditGuestBookPresetMessageItem ($_REQUEST['text'], $_REQUEST['lang_code'], $_REQUEST['id']);
#						$this->GuestBookPresetMessageModel->AddGuestBookPresetMessageItem ($_REQUEST['text'], $_REQUEST['lang_code']);
						$this->template->addWarningMesage ('Edit Preset Message Successful');
						$this->listPage ();
					} else {
						$text = NULL;
						$lang_code = NULL;
						if ($_REQUEST['text'] == '') {
							$this->template->addWarningMesage ('Please input the description');
						} else {
							$text = $_REQUEST['text'];
						}
						if ($_REQUEST['lang_code'] == '') {
							$this->template->addWarningMesage ('Please select the language code');
						} else if (!$this->LanguageModel->CheckLanguageExistsByCode ($_REQUEST['lang_code'])) {
							$this->template->addWarningMesage ('Please select the correct language code');
						} else {
							$lang_code = $_REQUEST['lang_code'];
						}
						$this->NaddPage ($text, $lang_code, $GuestBookPresetMessageItem[$this->GuestBookPresetMessageModel->idRowName]);
					}
				} else {
					$this->NaddPage ($GuestBookPresetMessageItem[$this->GuestBookPresetMessageModel->textRowName], $GuestBookPresetMessageItem[$this->GuestBookPresetMessageModel->langCodeRowName], $GuestBookPresetMessageItem[$this->GuestBookPresetMessageModel->idRowName]);
				}
			} else {
				$this->template->addWarningMesage ('Message not exists');
				$this->listPage ();
			}
		} else {
			$this->template->addWarningMesage ('wrong parameter');
		}
/*
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$this->LanguageModel = new LanguageModel ($this->mysql);
			if ($_REQUEST['text'] != '' && $_REQUEST['lang_code'] != '' && $this->LanguageModel->CheckLanguageExistsByCode ($_REQUEST['lang_code'])) {
				$this->GuestBookPresetMessageModel->AddGuestBookPresetMessageItem ($_REQUEST['text'], $_REQUEST['lang_code']);
				$this->template->addWarningMesage ('Add a new Preset Message Successful');
				$this->listPage ();
			} else {
				$text = NULL;
				$lang_code = NULL;
				if ($_REQUEST['text'] == '') {
					$this->template->addWarningMesage ('Please input the description');
				} else {
					$text = $_REQUEST['text'];
				}
				if ($_REQUEST['lang_code'] == '') {
					$this->template->addWarningMesage ('Please select the language code');
				} else if (!$this->LanguageModel->CheckLanguageExistsByCode ($_REQUEST['lang_code'])) {
					$this->template->addWarningMesage ('Please select the correct language code');
				} else {
					$lang_code = $_REQUEST['lang_code'];
				}
				$this->NaddPage ($text, $lang_code);
			}
		} else {
			$this->NaddPage ();
		}
*/
	}

	private function listPage () {
		$page = 0;
		$totalpage = 0;
		$totalrows = $this->GuestBookPresetMessageModel->GetGuestBookPresetMessageCount ();

		if (array_key_exists ('page', $_REQUEST) && ctype_digit ($_REQUEST['page'])) {
			$page = $_REQUEST['page'];
		}
		$totalpage = ceil ($totalrows / $this->diaplayrows);
		if ($page != 0 && $totalpage <= $page) {
			$page = 0;
			$this->template->addWarningMesage ('no record here');
		}
		if ($totalpage == 0) {
			$totalpage = 1;
		}
		$messagelisttemplate = new template ();
		$messagelisttemplate->load ('html/GuestBookPresetMessagelist.html');
		$messagelistform = $messagelisttemplate->dom->documentElement;
		$messagelistform = $this->template->dom->importNode ($messagelistform, true);

		$node = $this->template->query ('div[@class="buttonarea"]/input[@id="addPresetMessageButton"]', $messagelistform)->item (0);
		$node->setAttribute ('onClick', sprintf ('javascript:relocation(\'?module=%s&action=add\');', $this->Loader->module));

//		$node = $this->template->query ('tbody[@id="messagelistitem"]', $messagelistform)->item (0);
		$node = $this->template->query ('table/tbody[@id="messagelistitem"]', $messagelistform)->item (0);
		$messagelist = $node->parentNode;
#		$messagelistitem = $node->cloneNode (true);
		$messagelistitem = $node;
		$messagelist->removeChild ($node);

		foreach ($this->GuestBookPresetMessageModel->GetGuestBookPresetMessageItems ($page * $this->diaplayrows, $this->diaplayrows) as $GuestBookPresetMessageItem) {
			$item = $messagelistitem->cloneNode (true);

			$node = $this->template->query ('tr/td[@id="text"]/a', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($GuestBookPresetMessageItem[$this->GuestBookPresetMessageModel->textRowName]);
			$node->setAttribute ('href', sprintf ('?module=%s&action=edit&id=%s', $this->Loader->module, $GuestBookPresetMessageItem[$this->GuestBookPresetMessageModel->idRowName]));

			$node = $this->template->query ('tr/td[@id="language_code"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($GuestBookPresetMessageItem[$this->GuestBookPresetMessageModel->langCodeRowName]);
			$messagelist->appendChild ($item);

			$node = $this->template->query ('tr/td[@id="Delete"]/a', $item)->item (0);
			$node->setAttribute ('href', sprintf ('?module=%s&action=delete&id=%d', $this->Loader->module, $GuestBookPresetMessageItem[$this->GuestBookPresetMessageModel->idRowName]));
			$messagelist->appendChild ($item);
		}

		$node = $this->template->query ('span[@id="totlepage"]', $messagelistform)->item (0);
		$node->nodeValue = htmlspecialchars ($totalpage);

		$node = $this->template->query ('span[@id="page"]', $messagelistform)->item (0);
		$node->nodeValue = htmlspecialchars ($page + 1);

		$node = $this->template->query ('a[@id="previouspage"]', $messagelistform)->item (0);
		if ($page == 0) {
			$node->setAttribute ('href', '#');
		} else {
			$node->setAttribute ('href', sprintf ('?module=%s&page=%d', $this->Loader->module, $page - 1));
		}
		$node = $this->template->query ('a[@id="nextpage"]', $messagelistform)->item (0);
		if ($page == $totalpage - 1) {
			$node->setAttribute ('href', '#');
		} else {
			$node->setAttribute ('href', sprintf ('?module=%s&page=%d', $this->Loader->module, $page + 1));
		}

		$this->template->content->appendChild ($messagelistform);

	}
}
?>
